<%@ page import="org.bson.Document" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="com.mongodb.client.*" %>
<%@ page import="java.util.Date" %>
<%@ page import="com.mongodb.client.result.UpdateResult" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("UTF-8");
    //response.setCharacterEncoding("UTF-8");
    String sno = request.getParameter("sno");
    String name = request.getParameter("name");
    String sex = request.getParameter("sex");
    String age = request.getParameter("age");
    String idCard = request.getParameter("idCard");
    String birthDay = request.getParameter("birthDay");
    String phone = request.getParameter("phone");
    String major = request.getParameter("major");
    String email = request.getParameter("email");
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String address = request.getParameter("address");

    MongoClient conn = MongoClients.create("mongodb://g08:" + URLEncoder.encode("abc@8") + "@mongo.zhledu.cn:27017/g08");
    MongoDatabase g08 = conn.getDatabase("g08");
    MongoCollection<Document> student = g08.getCollection("student");
    Document update = new Document();
    Document where = new Document("sno", sno);
    if (name != null && name.length() > 0) {
        update.append("name", name);
    }
    if (sex != null && sex.length() > 0) {
        update.append("sex", sex);
    }
    if (age != null && age.length() > 0) {
        update.append("age", Integer.valueOf(age));
    }
    if (idCard != null && idCard.length() > 0) {
        update.append("idCard", idCard);
    }
    if (birthDay != null && birthDay.length() > 0) {
        update.append("birthDay", birthDay);
    }
    if (phone != null && phone.length() > 0) {
        update.append("phone", phone);
    }
    if (major != null && major.length() > 0) {
        update.append("major", major);
    }
    if (email != null && email.length() > 0) {
        update.append("email", email);
    }
    if (username != null && username.length() > 0) {
        update.append("username", username);
    }
    if (password != null && password.length() > 0) {
        update.append("password", password);
    }
    if (address != null && address.length() > 0) {
        update.append("address", address);
    }

    try {
        UpdateResult result = student.updateMany(where, new Document("$set", update));
        if (result.getModifiedCount() == 1) {
            out.print("修改成功,2秒后跳转到首页");
            conn.close();
            response.setHeader("refresh", "2;URL=index.jsp");
        } else {
            out.print("修改失败，没有找到学号为" + sno + "的学生,2秒后跳转到首页");
            conn.close();
            response.setHeader("refresh", "2;URL=index.jsp");
        }

    } catch (Exception e) {
        out.print("添加失败,2秒后跳转到添加页");
        conn.close();
        response.setHeader("refresh", "2;URL=add.jsp");
    } finally {
        conn.close();
    }


%>
